package com.example.tests;

import static com.example.tests.GroupDataGenerator.loadGroupsFromXmlFile;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import com.example.utils.SortedListOf;

import static com.example.tests.ContactDataGenerator.loadContactsFromCsvFile;
import static com.example.tests.ContactDataGenerator.loadContactsFromXmlFile;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class ContactCreationTests extends TestBase {
	@DataProvider
	public Iterator<Object[]> contactsFromFile() throws IOException {		
		return wrapContactsForDataProvider(loadContactsFromCsvFile(new File("contacts.txt"))).iterator();
	}
    
    @Test (dataProvider = "contactsFromFile")
    public void testNonEmptyContactCreation(ContactData contact) throws Exception {
        //save old state
    	SortedListOf<ContactData> oldList = app.getContactHelper().getContacts();
        
        // contact creation
        app.getContactHelper().createContact(contact);
                
        //save new state
        SortedListOf<ContactData> newList = app.getContactHelper().getContacts();
        
        // compare states  
        // 	compare internal states 
        assertThat(newList, equalTo(oldList.withAdded(contact)));
        //	compare cashed contacts and UI contacts 
        assertThat(newList, equalTo(app.getContactHelper().getUiContacts()));
    }
}
   